//
//  TopBar.m
//  QSBK
//
//  Created by Eric Wang on 4/17/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//
#import "TopBar.h"

@implementation TopBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews
{
    // alloc and init
    self.qiushi = [[UIButton alloc]init];
    self.qiushi.selected = YES;
    self.qiushi.tag = 997;
    self.selectedButton = self.qiushi;
    self.qiushi.translatesAutoresizingMaskIntoConstraints = NO;
    [self.qiushi setImage:[UIImage imageNamed:@"ic_qiushi_normal.png"] forState:UIControlStateNormal];
    [self.qiushi setImage:[UIImage imageNamed:@"ic_qiushi_select.png"] forState:UIControlStateSelected];
    [self.qiushi addTarget:self action:@selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.nearby = [[UIButton alloc]init];
    self.nearby.tag = 998;
    self.nearby.translatesAutoresizingMaskIntoConstraints = NO;
    [self.nearby setImage:[UIImage imageNamed:@"ic_nearby_normal.png"] forState:UIControlStateNormal];
    [self.nearby setImage:[UIImage imageNamed:@"ic_nearby_select.png"] forState:UIControlStateSelected];
    [self.nearby addTarget:self action:@selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.message = [[UIButton alloc]init];
    self.message.tag = 999;
    self.message.translatesAutoresizingMaskIntoConstraints = NO;
    [self.message setImage:[UIImage imageNamed:@"ic_message_normal.png"] forState:UIControlStateNormal];
    [self.message setImage:[UIImage imageNamed:@"ic_message_select.png"] forState:UIControlStateSelected];
    [self.message addTarget:self action:@selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.profile = [[UIButton alloc]init];
    self.profile .translatesAutoresizingMaskIntoConstraints = NO;
    [self.profile setImage:[UIImage imageNamed:@"unlogin_day_normal.png"] forState:UIControlStateNormal];
    [self.profile setImage:[UIImage imageNamed:@"unlogin_day_pressed.png"] forState:UIControlStateHighlighted];
    [self.profile addTarget:self action:@selector(requestLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    self.more = [[UIButton alloc]init];
    self.more.translatesAutoresizingMaskIntoConstraints = NO;
    [self.more setImage:[UIImage imageNamed:@"actionbar_over_flow_menu.png"] forState:UIControlStateNormal];
    [self.more addTarget:self action:@selector(moreMenu:) forControlEvents:UIControlEventTouchUpInside];
    
    self.refreshView = [[UIImageView alloc]init];
    self.refreshView.translatesAutoresizingMaskIntoConstraints = NO;
    self.refreshView.image = [UIImage imageNamed:@"spinner_24.png"];
    
    
    self.headImage = [[UIImageView alloc]init];
    self.headImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookUserInfo)];
    [self.headImage addGestureRecognizer:tap];
    [self.headImage.layer setCornerRadius:20.0];
    self.headImage.layer.masksToBounds = YES;
    self.headImage.translatesAutoresizingMaskIntoConstraints = NO;
   
    
    [self addSubview:self.qiushi];
    [self addSubview:self.nearby];
    [self addSubview:self.message];
    [self addSubview:self.headImage];
    [self addSubview:self.profile];
    [self addSubview:self.more];
    [self addSubview:self.refreshView];
    
    //autolayout
    _constraint = [[NSMutableArray alloc]init];
    
    CGFloat width = self.frame.size.height;
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[qiushi(width)]-(>=0)-|" options:0 metrics:@{@"width":@(width)} views:@{@"qiushi":self.qiushi}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[qiushi]-0-|" options:0 metrics:nil views:@{@"qiushi":self.qiushi}]];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[qiushi]-0-[nearby(width)]-(>=0)-|" options:0 metrics:@{@"width":@(width)} views:@{@"qiushi":self.qiushi,@"nearby":self.nearby}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[nearby]-0-|" options:0 metrics:nil views:@{@"nearby":self.nearby}]];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[nearby]-0-[message(width)]-(>=0)-|" options:0 metrics:@{@"width":@(width)} views:@{@"nearby":self.nearby,@"message":self.message}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[message]-0-|" options:0 metrics:nil views:@{@"message":self.message}]];
    
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[more(width)]-0-|" options:0 metrics:@{@"width":@(width)} views:@{@"more":self.more}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[more]-0-|" options:0 metrics:nil views:@{@"more":self.more}]];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[headImage(width)]-0-[more]" options:0 metrics:@{@"width":@(width)} views:@{@"headImage":self.headImage,@"more":self.more}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[headImage]-0-|" options:0 metrics:nil views:@{@"headImage":self.headImage}]];
    
    
    
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[profile(width)]-0-[more]" options:0 metrics:@{@"width":@(width)} views:@{@"profile":self.profile,@"more":self.more}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[profile]-0-|" options:0 metrics:nil views:@{@"profile":self.profile}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[refreshView(width)]-40-[profile]" options:0 metrics:@{@"width":@(width)} views:@{@"refreshView":self.refreshView,@"profile":self.profile}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[refresh]-0-|" options:0 metrics:nil views:@{@"refresh":self.refreshView}]];
    
    [self addConstraints:_constraint];
    //
    
    
    
    
    
    //animation
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1.5;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    
    [self.refreshView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

#pragma mark menuButton 事件

- (void)menuButtonClick:(UIButton *)button
{
    button.selected = YES;
    self.selectedButton.selected = NO;
    self.selectedButton = button;
    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(turnToOtherController:)])
    {
        [self.delegate turnToOtherController:button.tag];
    }
}

#pragma mark user to login;
- (void)requestLogin:(UIButton *)button
{
    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(turnToLogin)])
    {
        [self.delegate turnToLogin];
    }
    
}

#pragma mark More Menu
- (void)moreMenu:(UIButton *)button
{
    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(turnTOMoreChoices)])
    {
        [self.delegate turnTOMoreChoices];
    }
}

#pragma headImage tap gesture
-(void)lookUserInfo
{
    if(self.delegate!=nil&&[self.delegate respondsToSelector:@selector(lookUserInfo)])
    {
        [self.delegate lookUserInfo];
    }
}
@end
